﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Core.DataAccess.Model.Project.Entrepot;
using Core.Framework.Model.Common;
using Core.Framework.Util;
using Core.IBusiness.IEntrepotModule;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Core.Web.Controllers.Entrepot
{

    /// <summary>
    /// 字典项配置
    /// </summary>
    [Route(".v1/Entrepot/[controller]")]
    [ApiController]
    [ApiVerification(IBusiness.IProjectModule.Model.APIServiceEnum.Shop)]
    public class ProductParamsController : BaseApiController
    {
        public ProductParamsController(IEntrepot iEntrepot) :
            base(iEntrepot: iEntrepot)
        { }
        
        /// <summary>
        /// 获取字典title属性集合
        /// </summary>
        /// <param name="pagination">分页实体</param>
        /// <returns></returns>
        [HttpPost(".page")]
        [IgnoreAuthorize]
        public IResult GetOrderItem(Pagination pagination)
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.GerDicContent(
                        iProjectInfo.Token,
                        pagination), pagination);
                });
        }




        /// <summary>
        /// 获取指定字典内容
        /// </summary>
        /// <param name="param_key">属性关键字</param>
        /// <returns></returns>
        [HttpGet("{param_key}")]
        [IgnoreAuthorize]
        public IResult GerDicItemContent(int param_key)
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.GerDicItemContent(
                        param_key,
                        iProjectInfo.Token));
                },
                new Tuple<bool, string>(param_key > 0, param_key > 0 ? "" : "params: param_key is null"));
        }


        /// <summary>
        /// 添加字典
        /// </summary>
        /// <param name="model">字典实体</param>
        /// <returns></returns>
        [HttpPost]
        public IResult POST(ProductParams model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.DicTitleExec(
                        model,
                        SqlExecType.insert));
                }, model.IsValid(IsValidEnum.reg));
        }

        /// <summary>
        /// 添加指定字典内容
        /// </summary>
        /// <param name="model">字典内容实体</param>
        /// <returns></returns>
        [HttpPost(".dic")]
        public IResult POST(ProductParamsDic model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.DicContentExec(
                        model,
                        SqlExecType.insert));
                }, model.IsValid(IsValidEnum.reg));
        }





        /// <summary>
        /// 修改字典内容
        /// </summary>
        /// <param name="model">字典实体</param>
        /// <returns></returns>
        [HttpPut]
        public IResult PUT(ProductParams model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.DicTitleExec(
                        model,
                        SqlExecType.update));
                }, model.IsValid(IsValidEnum.update));
        }


        /// <summary>
        /// 修改指定字典内容
        /// </summary>
        /// <param name="model">字典内容实体</param>
        /// <returns></returns>
        [HttpPut(".dic")]
        public IResult PUT(ProductParamsDic model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.DicContentExec(
                        model,
                        SqlExecType.update));
                }, model.IsValid(IsValidEnum.update));
        }




        /// <summary>
        /// 删除字典内容
        /// </summary>
        /// <param name="id">字典关键字</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IResult Delete(int id)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.DicTitleExec(
                        new ProductParams
                        {
                            Id = id,
                            ProjectToken = iProjectInfo.Token
                        }, 
                        SqlExecType.delete));
                },
                new Tuple<bool, string>(id > 0, id > 0 ? "" : "params: id is null"));
        }


        /// <summary>
        /// 删除指定字典内容
        /// </summary>
        /// <param name="id">字典内容关键字</param>
        /// <returns></returns>
        [HttpDelete(".dic{id}")]
        public IResult DeleteDic(int id)
        {
            return this.ExecMethod(
                () =>
                {
                    return this.Result(this.iEntrepot.DicContentExec(
                            new ProductParamsDic
                            {
                                Id = id,
                                ProjectToken = iProjectInfo.Token
                            },
                            SqlExecType.delete)
                    );
                },
                new Tuple<bool, string>(id > 0, id > 0 ? "" : "params: id is null"));
        }

    }
}